<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('kefu.user.quick Search Fields')"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import { getArrayKey } from '/@/utils/common'

defineOptions({
    name: 'kefu/user',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/kefu.User/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('kefu.user.id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('kefu.user.identity'),
                prop: 'identity',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: { tourist: t('kefu.user.identity tourist'), user: t('kefu.user.identity user'), csr: t('kefu.user.identity csr') },
            },
            { label: t('kefu.user.group_ids'), prop: 'group.name', align: 'center', render: 'tags', operator: false },
            {
                label: t('kefu.user.group_ids'),
                prop: 'group_ids',
                align: 'center',
                operator: 'FIND_IN_SET',
                show: false,
                comSearchRender: 'remoteSelect',
                remote: { pk: 'group.id', field: 'name', remoteUrl: '/admin/kefu.Group/index', multiple: true },
            },
            {
                show: false,
                label: t('kefu.user.nickname'),
                prop: 'id',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: '=',
                comSearchRender: 'remoteSelect',
                remote: {
                    pk: 'user.id',
                    field: 'nickname',
                    remoteUrl: '/admin/kefu.User/index',
                },
            },
            {
                label: t('kefu.user.nickname'),
                prop: 'nickname',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: false,
                sortable: false,
            },
            {
                label: t('kefu.user.referrer'),
                prop: 'referrer',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                show: false,
            },
            {
                label: t('kefu.user.gender'),
                prop: 'gender',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: { '0': t('kefu.user.gender 0'), '1': t('kefu.user.gender 1'), '2': t('kefu.user.gender 2') },
                show: false,
            },
            {
                label: t('kefu.user.contacts'),
                prop: 'contacts',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                showOverflowTooltip: true,
            },
            {
                label: t('kefu.user.company'),
                prop: 'company',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                showOverflowTooltip: true,
            },
            {
                label: t('kefu.user.occupation'),
                prop: 'occupation',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                showOverflowTooltip: true,
                show: false,
            },
            {
                label: t('kefu.user.last_login_time'),
                prop: 'last_login_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('kefu.user.last_login_ip'),
                prop: 'last_login_ip',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
            },
            {
                label: t('kefu.user.join_time'),
                prop: 'join_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
                show: false,
            },
            {
                label: t('kefu.user.join_ip'),
                prop: 'join_ip',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                show: false,
            },
            {
                label: t('kefu.user.csr_maximum_reception'),
                prop: 'csr_maximum_reception',
                align: 'center',
                operator: 'RANGE',
                sortable: false,
                show: false,
            },
            {
                label: t('kefu.user.csr_reception_count'),
                prop: 'csr_reception_count',
                align: 'center',
                operator: 'RANGE',
                sortable: false,
            },
            {
                label: t('kefu.user.csr_last_reception_time'),
                prop: 'csr_last_reception_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
                show: false,
            },
            {
                label: t('kefu.user.status'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: {
                    offline: t('kefu.user.status offline'),
                    online: t('kefu.user.status online'),
                    busy: t('kefu.user.status busy'),
                    invisible: t('kefu.user.status invisible'),
                },
            },
            {
                label: t('kefu.user.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            identity: 'tourist',
            gender: '0',
            last_login_time: null,
            join_time: null,
            csr_maximum_reception: 0,
            csr_reception_count: 0,
            csr_last_reception_time: null,
            status: 'offline',
        },
    },
    {
        onTableAction({ event, data }) {
            if (event == 'com-search') {
                // 当公共搜索了某字段，但该字段未在表格显示时
                const checkColumn = ['referrer', 'gender', 'occupation', 'join_time', 'join_ip', 'csr_maximum_reception', 'csr_last_reception_time']
                for (const key in data) {
                    if (checkColumn.includes(data[key].field)) {
                        const columnKey = getArrayKey(baTable.table.column, 'prop', data[key].field)
                        if (columnKey !== false && !baTable.table.column[columnKey].show) {
                            // 切换列未显示状态
                            baTable.table.column[columnKey].show = true
                        }
                    }
                }
            }
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
